<template>
  <com-header title="商品详情"></com-header>
  <section class="good">
    <div class="banner">
      <img :src="good.pict_url" alt="" />
    </div>
    <van-row type="flex" align="center" class="title padded-10" :gutter="10">
      <van-col :span="3">
        <div class="tags text-center ftn12 text-f">
          {{ good.user_type == "1" ? "天猫" : "淘宝" }}
        </div>
      </van-col>
      <van-col :span="21">
        <div class="title van-ellipsis ftn14">{{ good.title }}</div>
      </van-col>
    </van-row>
    <van-row
      type="flex"
      justify="space-between"
      align="center"
      class="price p-l-10 p-r-10"
      :gutter="10"
    >
      <van-col :span="8" class="price-lt">
        <div class="jiaqian ftn18">
          <span class="ftn12">￥</span>
          {{ good.quanhou_jiage }}
        </div>
        <div class="tags ftn12">券后价</div>
      </van-col>
      <van-col :span="8" class="ftn13 text-3 text-center"> 包邮 </van-col>
      <van-col :span="8" class="ftn14 juan text-right">
        {{ d_price(good.coupon_info_money) }}元券
      </van-col>
    </van-row>
  </section>
  <section class="tuijian">
    <div class="text-center ftn16 text-green padded-10">为您推荐</div>
    <van-row justify="center">
      <van-col :span="12" v-for="(value, index) in newGoods" :key="index">
        <Good :item="value"></Good>
      </van-col>
    </van-row>
  </section>
  <van-sticky position="bottom" offset-bottom="0">
    <footer class="bg-f p-r-10">
        <van-row type="fixed" justify="space-between" align="center">
            <van-col :span="4" class="text-center">
                <van-icon name="chat-o"  size="20"/>
                <div class="text-center ftn12">客服</div>
            </van-col>
            <van-col :span="4" class="text-center" @click="goCart">
                <van-badge :content="cart.num">
                    <van-icon name="shopping-cart-o" size="20"/>
                </van-badge>
                <div class="text-center ftn12">购物车</div>
                
            </van-col>
            <van-col :span="16" class="text-right">
                <van-button type="primary" @click="addCart" class="m-r-10">加入购物车</van-button>
                <van-button type="primary" @click="goPay">立即购买</van-button>
            </van-col>
        </van-row>
    </footer>
  </van-sticky>
</template>
<script lang="ts">
import { defineComponent, onMounted, ref, defineAsyncComponent } from "vue";
import { useRoute, useRouter, onBeforeRouteUpdate } from "vue-router";
import { goodDetail, goodTui } from "@/service/good";
import type { IGood, ITui, ICart } from "@/types/index";
import { useStore } from "@/stores/store";
import { fnMsg } from "@/plugins/common";
import router from "@/router";
export default defineComponent({
    components: {
        Good: defineAsyncComponent(() => import('@/views/good/component/list.vue')),
    },
  setup() {
    const store = useStore(),
     route = useRoute(),
      good = ref<IGood>({
        pict_url: "",
        title: "",
        coupon_info_money: "",
        tkl: "",
        tao_id: "",
        user_type: "",
        quanhou_jiage: "",
        num: 1
      }),
      newGoods = ref<ITui[]>([]), cart = ref({
        num: store.state.cart.num,
        goods: store.state.cart.good
      })
    const d_price = (price: String): String => {
      return price.substring(0, price.indexOf("."));
    };
    const tuiDood = () => {
        goodTui().then(res => {
            if (res) {
                newGoods.value = res.content;
            }
        })
    }
    // 加入购物车
    const addCart = () => {
      // store.commit('editCart', {num: 0, good: []});
      let {num, goods} = cart.value;
      console.log(goods, '000000')
      let index = goods.findIndex((item: IGood) => item.tao_id == good.value.tao_id);
      console.log(index, num)
      if (index < 0) {
        num++;
        good.value.num = 1;
        goods.push(good.value);
        cart.value = {num, goods}
        store.commit('editCart', {num, good: goods});
        fnMsg('添加成功');
      } else {
        fnMsg('商品已存在，无需重复添加');
        return;
      }
    }

    // 更新商品详情
    onBeforeRouteUpdate(to => {
      if (to.name == 'good') {
        getDetail(to.params.id);
        setTimeout(() => {
          window.scrollTo(0,0)
        }, 500)
        
      }
    })

    // 购物车
    const goCart = () => {
      router.push({
        name: 'cart'
      })
    }

    const getDetail = (id: any) => {
      goodDetail({ id: id }).then((res) => {
        if (res) {
          good.value = res;
          console.log(good.value);
        }
      });
      tuiDood();
    }
    // 去结算
    const goPay = () => {
      store.commit("addOrder", { list: [good.value], total_price: good.value.quanhou_jiage});
      router.push({
        name:
          store.state.addressData.list.length == 0 ? "address" : "create_order",
      });
    };
    onMounted(() => {
      getDetail(route.params.id)
      console.log(route.params);
    });
    return {
      good,
      d_price,
      newGoods,
      cart,
      addCart,
      goCart,
      goPay
    };
  },
});
</script>
<style lang="less">
.good {
  .banner {
    position: relative;
    width: 100%;
    height: 0;
    padding-bottom: 100%;
    overflow: hidden;
    img {
      width: 100%;
    }
  }
  .title {
    .tags {
      background-color: @primary-color;
      padding: 2px 0;
      border-radius: 3px;
    }
  }
  .price {
    .price-lt {
      display: flex;
      align-items: center;
      .jiaqian {
        color: @primary-color;
      }
      .tags {
        border: 1px solid @primary-color;
        color: @primary-color;
        margin-left: 10px;
        border-radius: 2px;
      }
    }
    .juan {
      color: @primary-color;
    }
  }
}
footer {
    padding: 5px 0;
    .cart {
        position: relative;
        .num {
            position: absolute;
            right: 25%;
            top: 0;
            background-color: #f00;
            color: #fff;
            border-radius: 5px;
            font-size: 12px;
        }
    }
}
</style>
